home *** CD-ROM | disk | FTP | other *** search
/ American Osteopathic Ass…tion Yearbook 2005 & 2006 / American Osteopathic Association Yearbook 2005 & 2006.iso / mac / app / traceback.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-07-22  |  13.4 KB  |  355 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. '''Extract, format and print information about Python stack traces.'''
  5. import linecache
  6. import sys
  7. import types
  8. __all__ = [
  9.     'extract_stack',
  10.     'extract_tb',
  11.     'format_exception',
  12.     'format_exception_only',
  13.     'format_list',
  14.     'format_stack',
  15.     'format_tb',
  16.     'print_exc',
  17.     'print_exception',
  18.     'print_last',
  19.     'print_stack',
  20.     'print_tb',
  21.     'tb_lineno']
  22.  
  23. def _print(file, str = '', terminator = '\n'):
  24.     file.write(str + terminator)
  25.  
  26.  
  27. def print_list(extracted_list, file = None):
  28.     '''Print the list of tuples as returned by extract_tb() or
  29.     extract_stack() as a formatted stack trace to the given file.'''
  30.     if file is None:
  31.         file = sys.stderr
  32.     
  33.     for filename, lineno, name, line in extracted_list:
  34.         _print(file, '  File "%s", line %d, in %s' % (filename, lineno, name))
  35.         if line:
  36.             _print(file, '    %s' % line.strip())
  37.             continue
  38.     
  39.  
  40.  
  41. def format_list(extracted_list):
  42.     '''Format a list of traceback entry tuples for printing.
  43.  
  44.     Given a list of tuples as returned by extract_tb() or
  45.     extract_stack(), return a list of strings ready for printing.
  46.     Each string in the resulting list corresponds to the item with the
  47.     same index in the argument list.  Each string ends in a newline;
  48.     the strings may contain internal newlines as well, for those items
  49.     whose source text line is not None.
  50.     '''
  51.     list = []
  52.     for filename, lineno, name, line in extracted_list:
  53.         item = '  File "%s", line %d, in %s\n' % (filename, lineno, name)
  54.         if line:
  55.             item = item + '    %s\n' % line.strip()
  56.         
  57.         list.append(item)
  58.     
  59.     return list
  60.  
  61.  
  62. def print_tb(tb, limit = None, file = None):
  63.     """Print up to 'limit' stack trace entries from the traceback 'tb'.
  64.  
  65.     If 'limit' is omitted or None, all entries are printed.  If 'file'
  66.     is omitted or None, the output goes to sys.stderr; otherwise
  67.     'file' should be an open file or file-like object with a write()
  68.     method.
  69.     """
  70.     if file is None:
  71.         file = sys.stderr
  72.     
  73.     if limit is None:
  74.         if hasattr(sys, 'tracebacklimit'):
  75.             limit = sys.tracebacklimit
  76.         
  77.     
  78.     n = 0
  79.     while tb is not None and limit is None or n < limit:
  80.         f = tb.tb_frame
  81.         lineno = tb.tb_lineno
  82.         co = f.f_code
  83.         filename = co.co_filename
  84.         name = co.co_name
  85.         _print(file, '  File "%s", line %d, in %s' % (filename, lineno, name))
  86.         line = linecache.getline(filename, lineno)
  87.         if line:
  88.             _print(file, '    ' + line.strip())
  89.         
  90.         tb = tb.tb_next
  91.         n = n + 1
  92.  
  93.  
  94. def format_tb(tb, limit = None):
  95.     """A shorthand for 'format_list(extract_stack(f, limit))."""
  96.     return format_list(extract_tb(tb, limit))
  97.  
  98.  
  99. def extract_tb(tb, limit = None):
  100.     """Return list of up to limit pre-processed entries from traceback.
  101.  
  102.     This is useful for alternate formatting of stack traces.  If
  103.     'limit' is omitted or None, all entries are extracted.  A
  104.     pre-processed stack trace entry is a quadruple (filename, line
  105.     number, function name, text) representing the information that is
  106.     usually printed for a stack trace.  The text is a string with
  107.     leading and trailing whitespace stripped; if the source is not
  108.     available it is None.
  109.     """
  110.     if limit is None:
  111.         if hasattr(sys, 'tracebacklimit'):
  112.             limit = sys.tracebacklimit
  113.         
  114.     
  115.     list = []
  116.     n = 0
  117.     while tb is not None and limit is None or n < limit:
  118.         f = tb.tb_frame
  119.         lineno = tb.tb_lineno
  120.         co = f.f_code
  121.         filename = co.co_filename
  122.         name = co.co_name
  123.         line = linecache.getline(filename, lineno)
  124.         if line:
  125.             line = line.strip()
  126.         else:
  127.             line = None
  128.         list.append((filename, lineno, name, line))
  129.         tb = tb.tb_next
  130.         n = n + 1
  131.     return list
  132.  
  133.  
  134. def print_exception(etype, value, tb, limit = None, file = None):
  135.     '''Print exception up to \'limit\' stack trace entries from \'tb\' to \'file\'.
  136.  
  137.     This differs from print_tb() in the following ways: (1) if
  138.     traceback is not None, it prints a header "Traceback (most recent
  139.     call last):"; (2) it prints the exception type and value after the
  140.     stack trace; (3) if type is SyntaxError and value has the
  141.     appropriate format, it prints the line where the syntax error
  142.     occurred with a caret on the next line indicating the approximate
  143.     position of the error.
  144.     '''
  145.     if file is None:
  146.         file = sys.stderr
  147.     
  148.     if tb:
  149.         _print(file, 'Traceback (most recent call last):')
  150.         print_tb(tb, limit, file)
  151.     
  152.     lines = format_exception_only(etype, value)
  153.     for line in lines[:-1]:
  154.         _print(file, line, ' ')
  155.     
  156.     _print(file, lines[-1], '')
  157.  
  158.  
  159. def format_exception(etype, value, tb, limit = None):
  160.     '''Format a stack trace and the exception information.
  161.  
  162.     The arguments have the same meaning as the corresponding arguments
  163.     to print_exception().  The return value is a list of strings, each
  164.     ending in a newline and some containing internal newlines.  When
  165.     these lines are concatenated and printed, exactly the same text is
  166.     printed as does print_exception().
  167.     '''
  168.     if tb:
  169.         list = [
  170.             'Traceback (most recent call last):\n']
  171.         list = list + format_tb(tb, limit)
  172.     else:
  173.         list = []
  174.     list = list + format_exception_only(etype, value)
  175.     return list
  176.  
  177.  
  178. def format_exception_only(etype, value):
  179.     '''Format the exception part of a traceback.
  180.  
  181.     The arguments are the exception type and value such as given by
  182.     sys.last_type and sys.last_value. The return value is a list of
  183.     strings, each ending in a newline.  Normally, the list contains a
  184.     single string; however, for SyntaxError exceptions, it contains
  185.     several lines that (when printed) display detailed information
  186.     about where the syntax error occurred.  The message indicating
  187.     which exception occurred is the always last string in the list.
  188.     '''
  189.     list = []
  190.     if type(etype) == types.ClassType:
  191.         stype = etype.__name__
  192.     else:
  193.         stype = etype
  194.     if value is None:
  195.         list.append(str(stype) + '\n')
  196.     elif etype is SyntaxError:
  197.         
  198.         try:
  199.             (filename, lineno, offset, line) = (msg,)
  200.         except:
  201.             pass
  202.  
  203.         if not filename:
  204.             filename = '<string>'
  205.         
  206.         list.append('  File "%s", line %d\n' % (filename, lineno))
  207.         if line is not None:
  208.             i = 0
  209.             while i < len(line) and line[i].isspace():
  210.                 i = i + 1
  211.             list.append('    %s\n' % line.strip())
  212.             if offset is not None:
  213.                 s = '    '
  214.                 for c in line[i:offset - 1]:
  215.                     if c.isspace():
  216.                         s = s + c
  217.                         continue
  218.                     s = s + ' '
  219.                 
  220.                 list.append('%s^\n' % s)
  221.             
  222.             value = msg
  223.         
  224.     
  225.     s = _some_str(value)
  226.     if s:
  227.         list.append('%s: %s\n' % (str(stype), s))
  228.     else:
  229.         list.append('%s\n' % str(stype))
  230.     return list
  231.  
  232.  
  233. def _some_str(value):
  234.     
  235.     try:
  236.         return str(value)
  237.     except:
  238.         return '<unprintable %s object>' % type(value).__name__
  239.  
  240.  
  241.  
  242. def print_exc(limit = None, file = None):
  243.     """Shorthand for 'print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, limit, file)'.
  244.     (In fact, it uses sys.exc_info() to retrieve the same information
  245.     in a thread-safe way.)"""
  246.     if file is None:
  247.         file = sys.stderr
  248.     
  249.     
  250.     try:
  251.         (etype, value, tb) = sys.exc_info()
  252.         print_exception(etype, value, tb, limit, file)
  253.     finally:
  254.         etype = value = tb = None
  255.  
  256.  
  257.  
  258. def print_last(limit = None, file = None):
  259.     """This is a shorthand for 'print_exception(sys.last_type,
  260.     sys.last_value, sys.last_traceback, limit, file)'."""
  261.     if file is None:
  262.         file = sys.stderr
  263.     
  264.     print_exception(sys.last_type, sys.last_value, sys.last_traceback, limit, file)
  265.  
  266.  
  267. def print_stack(f = None, limit = None, file = None):
  268.     """Print a stack trace from its invocation point.
  269.  
  270.     The optional 'f' argument can be used to specify an alternate
  271.     stack frame at which to start. The optional 'limit' and 'file'
  272.     arguments have the same meaning as for print_exception().
  273.     """
  274.     if f is None:
  275.         
  276.         try:
  277.             raise ZeroDivisionError
  278.         except ZeroDivisionError:
  279.             f = sys.exc_info()[2].tb_frame.f_back
  280.         except:
  281.             None<EXCEPTION MATCH>ZeroDivisionError
  282.         
  283.  
  284.     None<EXCEPTION MATCH>ZeroDivisionError
  285.     print_list(extract_stack(f, limit), file)
  286.  
  287.  
  288. def format_stack(f = None, limit = None):
  289.     """Shorthand for 'format_list(extract_stack(f, limit))'."""
  290.     if f is None:
  291.         
  292.         try:
  293.             raise ZeroDivisionError
  294.         except ZeroDivisionError:
  295.             f = sys.exc_info()[2].tb_frame.f_back
  296.         except:
  297.             None<EXCEPTION MATCH>ZeroDivisionError
  298.         
  299.  
  300.     None<EXCEPTION MATCH>ZeroDivisionError
  301.     return format_list(extract_stack(f, limit))
  302.  
  303.  
  304. def extract_stack(f = None, limit = None):
  305.     """Extract the raw traceback from the current stack frame.
  306.  
  307.     The return value has the same format as for extract_tb().  The
  308.     optional 'f' and 'limit' arguments have the same meaning as for
  309.     print_stack().  Each item in the list is a quadruple (filename,
  310.     line number, function name, text), and the entries are in order
  311.     from oldest to newest stack frame.
  312.     """
  313.     if f is None:
  314.         
  315.         try:
  316.             raise ZeroDivisionError
  317.         except ZeroDivisionError:
  318.             f = sys.exc_info()[2].tb_frame.f_back
  319.         except:
  320.             None<EXCEPTION MATCH>ZeroDivisionError
  321.         
  322.  
  323.     None<EXCEPTION MATCH>ZeroDivisionError
  324.     if limit is None:
  325.         if hasattr(sys, 'tracebacklimit'):
  326.             limit = sys.tracebacklimit
  327.         
  328.     
  329.     list = []
  330.     n = 0
  331.     while f is not None and limit is None or n < limit:
  332.         lineno = f.f_lineno
  333.         co = f.f_code
  334.         filename = co.co_filename
  335.         name = co.co_name
  336.         line = linecache.getline(filename, lineno)
  337.         if line:
  338.             line = line.strip()
  339.         else:
  340.             line = None
  341.         list.append((filename, lineno, name, line))
  342.         f = f.f_back
  343.         n = n + 1
  344.     list.reverse()
  345.     return list
  346.  
  347.  
  348. def tb_lineno(tb):
  349.     '''Calculate correct line number of traceback given in tb.
  350.  
  351.     Obsolete in 2.3.
  352.     '''
  353.     return tb.tb_lineno
  354.  
  355.